package com.fastloan.mgt.chatapp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.mgt.chatapp.common.ChatAppConstant;
import com.fastloan.mgt.chatapp.dao.CusManagerTempMapper;
import com.fastloan.mgt.chatapp.model.CusManagerTemp;
import com.fastloan.mgt.chatapp.service.ChatAppService;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.utils.HttpUtil;
import com.fastloan.utils.WeixinUtil;
import com.kawins.base.BaseMapper;
import com.kawins.base.baseabs.BaseServiceImpl;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.spring.ContextProperty;
import okhttp3.Response;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-12-2017/12/8 11:00
 */
@Service
public class ChatAppServiceImpl extends BaseServiceImpl<CusManagerTemp> implements ChatAppService {

    private static final String appId = (String) ContextProperty.getContextProperty("chatapp.customer.app_id");
    private static final String secret = (String) ContextProperty.getContextProperty("chatapp.customer.appSecret");

    @Autowired
    private CusManagerTempMapper cusManagerTempMapper;

    @Autowired
    private ZyCustomerService customerService;

    @Override
    protected BaseMapper<CusManagerTemp> buildDao() {
        return cusManagerTempMapper;
    }

    @Override
    public ResultTo bindManager(String managerId, String customerId) {
        ZyCustomer zyCustomer = customerService.selectByPrimaryKey(customerId);
        if (zyCustomer == null) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "客户不存在");
        }
        zyCustomer.setCustomerManagerId(managerId);
        customerService.updateByPrimaryKeySelective(zyCustomer);
        return new ResultTo();
    }

    @Override
    public ResultTo buildQrcode(String scene, String path) {
        String accessToken = WeixinUtil.getAccessToken(appId, secret);
        if (accessToken != null) {
            String url = ChatAppConstant.CODE_UNLIMIT + "?access_token=" + accessToken;
            Map<String, Object> map = new HashMap<>();
            map.put("scene", scene);
            map.put("path", path);
            Response response = HttpUtil.postByte(url, JSON.toJSONString(map));
            byte[] bytes = new byte[0];
            try {
                bytes = response.body().bytes();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String string = Base64.encodeBase64String(bytes);
            return new ResultTo().setData(string);
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    private JSONObject buildOpenId(String code) {
        StringBuffer stringBuffer = new StringBuffer(WeixinUtil.JSCODE_SESSION);
        stringBuffer.append("&appid=").append(appId).append("&secret=").append(secret).append("&js_code=").append(code);
        String s = HttpUtil.get(stringBuffer.toString());
        return JSONObject.parseObject(s);
    }

}
